Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.FFT = void 0;

exports.FFT = class {
    constructor(t) {
        if (this.size = 0 | t, this.size <= 1 || 0 != (this.size & this.size - 1)) throw new Error("FFT size must be a power of two and bigger than 1");
        this._csize = t << 1;
        const s = new Array(2 * this.size);
        for (let t = 0; t < s.length; t += 2) {
            const r = Math.PI * t / this.size;
            s[t] = Math.cos(r), s[t + 1] = -Math.sin(r);
        }
        this.table = s;
        let r = 0;
        for (let t = 1; this.size > t; t <<= 1) r += 1;
        this._width = r % 2 == 0 ? r - 1 : r, this._bitrev = new Array(1 << this._width);
        for (let t = 0; t < this._bitrev.length; t++) {
            this._bitrev[t] = 0;
            for (let s = 0; s < this._width; s += 2) {
                const r = this._width - s - 2;
                this._bitrev[t] |= (t >>> s & 3) << r;
            }
        }
        this._out = null, this._data = null, this._inv = 0;
    }
    fromComplexArray(t, s) {
        const r = s || new Array(t.length >>> 1);
        for (let s = 0; s < t.length; s += 2) r[s >>> 1] = t[s];
        return r;
    }
    createComplexArray() {
        const t = new Array(this._csize);
        for (let s = 0; s < t.length; s += 1) t[s] = 0;
        return t;
    }
    toComplexArray(t, s) {
        const r = s || this.createComplexArray();
        for (let s = 0; s < r.length; s += 2) r[s] = t[s >>> 1], r[s + 1] = 0;
        return r;
    }
    completeSpectrum(t) {
        const s = this._csize, r = s >>> 1;
        for (let i = 2; i < r; i += 2) t[s - i] = t[i], t[s - i + 1] = -t[i + 1];
    }
    transform(t, s) {
        if (t === s) throw new Error("Input and output buffers must be different");
        this._out = t, this._data = s, this._inv = 0, this._transform4(), this._out = null, 
        this._data = null;
    }
    realTransform(t, s) {
        if (t === s) throw new Error("Input and output buffers must be different");
        this._out = t, this._data = s, this._inv = 0, this._realTransform4(), this._out = null, 
        this._data = null;
    }
    inverseTransform(t, s) {
        if (t === s) throw new Error("Input and output buffers must be different");
        this._out = t, this._data = s, this._inv = 1, this._transform4();
        for (let s = 0; s < t.length; s++) t[s] /= this.size;
        this._out = null, this._data = null;
    }
    _transform4() {
        const t = this._out, s = this._csize;
        if (!t) return;
        let r, i, o = 1 << this._width, e = s / o << 1;
        const n = this._bitrev;
        if (4 === e) for (r = 0, i = 0; r < s; r += e, i++) {
            const t = n[i];
            this._singleTransform2(r, t, o);
        } else for (r = 0, i = 0; r < s; r += e, i++) {
            const t = n[i];
            this._singleTransform4(r, t, o);
        }
        const h = this._inv ? -1 : 1, {table: a} = this;
        for (o >>= 2; o >= 2; o >>= 2) {
            e = s / o << 1;
            const i = e >>> 2;
            for (r = 0; r < s; r += e) {
                const s = r + i;
                for (let e = r, n = 0; e < s; e += 2, n += o) {
                    const s = e, r = s + i, o = r + i, f = o + i, l = t[s], _ = t[s + 1], c = t[r], u = t[r + 1], m = t[o], d = t[o + 1], w = t[f], b = t[f + 1], g = l, p = _, v = a[n], T = h * a[n + 1], z = c * v - u * T, y = c * T + u * v, A = a[2 * n], x = h * a[2 * n + 1], E = m * A - d * x, F = m * x + d * A, C = a[3 * n], I = h * a[3 * n + 1], M = w * C - b * I, R = w * I + b * C, P = g + E, j = p + F, O = g - E, S = p - F, k = z + M, q = y + R, B = h * (z - M), D = h * (y - R), G = P + k, H = j + q, J = P - k, K = j - q, L = O + D, N = S - B, Q = O - D, U = S + B;
                    t[s] = G, t[s + 1] = H, t[r] = L, t[r + 1] = N, t[o] = J, t[o + 1] = K, t[f] = Q, 
                    t[f + 1] = U;
                }
            }
        }
    }
    _singleTransform2(t, s, r) {
        const i = this._out, o = this._data;
        if (!i || !o) return;
        const e = o[s], n = o[s + 1], h = o[s + r], a = o[s + r + 1], f = e + h, l = n + a, _ = e - h, c = n - a;
        i[t] = f, i[t + 1] = l, i[t + 2] = _, i[t + 3] = c;
    }
    _singleTransform4(t, s, r) {
        const i = this._out, o = this._data;
        if (!i || !o) return;
        const e = this._inv ? -1 : 1, n = 2 * r, h = 3 * r, a = o[s], f = o[s + 1], l = o[s + r], _ = o[s + r + 1], c = o[s + n], u = o[s + n + 1], m = o[s + h], d = o[s + h + 1], w = a + c, b = f + u, g = a - c, p = f - u, v = l + m, T = _ + d, z = e * (l - m), y = e * (_ - d), A = w + v, x = b + T, E = g + y, F = p - z, C = w - v, I = b - T, M = g - y, R = p + z;
        i[t] = A, i[t + 1] = x, i[t + 2] = E, i[t + 3] = F, i[t + 4] = C, i[t + 5] = I, 
        i[t + 6] = M, i[t + 7] = R;
    }
    _realTransform4() {
        const t = this._out;
        if (!t) throw new Error("transform运算出错");
        const s = this._csize;
        let r, i, o = 1 << this._width, e = s / o << 1;
        const n = this._bitrev;
        if (4 === e) for (r = 0, i = 0; r < s; r += e, i++) {
            const t = n[i];
            this._singleRealTransform2(r, t >>> 1, o >>> 1);
        } else for (r = 0, i = 0; r < s; r += e, i++) {
            const t = n[i];
            this._singleRealTransform4(r, t >>> 1, o >>> 1);
        }
        const h = this._inv ? -1 : 1, {table: a} = this;
        for (o >>= 2; o >= 2; o >>= 2) {
            e = s / o << 1;
            const i = e >>> 1, n = i >>> 1, f = n >>> 1;
            for (r = 0; r < s; r += e) for (let s = 0, e = 0; s <= f; s += 2, e += o) {
                const o = r + s, l = o + n, _ = l + n, c = _ + n, u = t[o], m = t[o + 1], d = t[l], w = t[l + 1], b = t[_], g = t[_ + 1], p = t[c], v = t[c + 1], T = u, z = m, y = a[e], A = h * a[e + 1], x = d * y - w * A, E = d * A + w * y, F = a[2 * e], C = h * a[2 * e + 1], I = b * F - g * C, M = b * C + g * F, R = a[3 * e], P = h * a[3 * e + 1], j = p * R - v * P, O = p * P + v * R, S = T + I, k = z + M, q = T - I, B = z - M, D = x + j, G = E + O, H = h * (x - j), J = h * (E - O), K = S + D, L = k + G, N = q + J, Q = B - H;
                if (t[o] = K, t[o + 1] = L, t[l] = N, t[l + 1] = Q, 0 === s) {
                    const s = S - D, r = k - G;
                    t[_] = s, t[_ + 1] = r;
                    continue;
                }
                if (s === f) continue;
                const U = q + -h * J, V = -B + -h * H, W = S + -h * D, X = -k - -h * G, Y = r + n - s, Z = r + i - s;
                t[Y] = U, t[Y + 1] = V, t[Z] = W, t[Z + 1] = X;
            }
        }
    }
    _singleRealTransform2(t, s, r) {
        const i = this._out, o = this._data;
        if (!i || !o) throw new Error("transform出错");
        const e = o[s], n = o[s + r], h = e + n, a = e - n;
        i[t] = h, i[t + 1] = 0, i[t + 2] = a, i[t + 3] = 0;
    }
    _singleRealTransform4(t, s, r) {
        const i = this._out, o = this._data;
        if (!i || !o) throw new Error("transform运行出错");
        const e = this._inv ? -1 : 1, n = 2 * r, h = 3 * r, a = o[s], f = o[s + r], l = o[s + n], _ = o[s + h], c = a + l, u = a - l, m = f + _, d = e * (f - _), w = c + m, b = u, g = -d, p = c - m, v = u, T = d;
        i[t] = w, i[t + 1] = 0, i[t + 2] = b, i[t + 3] = g, i[t + 4] = p, i[t + 5] = 0, 
        i[t + 6] = v, i[t + 7] = T;
    }
};